﻿
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ShijianJiluUIController : MonoBehaviour {

    public static ShijianJiluUIController _instance;
    public Transform tf_AllUI;
    private int i_AllUIChildCount;
    public Scrollbar sb_Content;            //scrollBar,用来控制界面内容的上下查看(程控)
    public Scrollbar sb_ContentSK;          //scrollBar,用来控制界面内容的上下查看(手控)
    private float f_contentValue = 1;       //根据这个值进行上下移动
    public Transform tf_IduanDongzuo;       //I段动作父集
    public Transform tf_IIduanDongzuo;      //II段动作父集
    public Transform tf_IIIduanDongzuo;     //III段动作父集
    public Text t_date;                     //事件发生的时间（格式:month-day hour:minute:second）
    public bool b_isSet;                    //三段电流跟时间是否设置完成
    public bool b_canStart;
    public float f_IduanTime;
    public float f_IIduanTime;
    public float f_IIIduanTime;
    public float f_HidedNum;                //在ScrollView的Content中被隐藏对象的个数
    public GameObject go_Prefab;

    // Use this for initialization
    void Start () {
        _instance = this;
        i_AllUIChildCount = tf_AllUI.childCount;
        EventManager<bool>.AddEvent(MyEventType.UpButtonClickEvent, OnUpButtonClick);
        EventManager<bool>.AddEvent(MyEventType.DownButtonClickEvent, OnDownButtonClick);
        EventManager<bool>.AddEvent(MyEventType.SetButtonClickEvent, OnSetButtonClick);
        EventManager<bool>.AddEvent(MyEventType.QuitButtonClickEvent, OnQuitButtonClick);
        EventManager<bool>.AddEvent(MyEventType.ChongzhiShujuEvent,Reset);
    }

    private void Reset(bool obj)
    {
        if (tf_AllUI.GetChild(i_AllUIChildCount - 1).name == "shijianjiluGO") {
            f_contentValue = 1;
            sb_Content.value = f_contentValue;
            sb_ContentSK.value = 1;
        }
    }

    void Update() {
        if (b_isSet) {
            b_isSet = false;
            //GetDianliu();
            b_canStart = true;
        }
    }

    /// <summary>
    /// 获取设置的三段电流大小跟时间
    /// </summary>
    public void GetDianliu() {
        System.Random ran = new System.Random();
//         tf_IduanDongzuo.GetChild(0).GetComponent<Text>().text = PinjieDianliu(11) + "A " + "0.011A 0.002A";
//         tf_IIduanDongzuo.GetChild(0).GetComponent<Text>().text = PinjieDianliu(13) + "A " + "0.002A 0.000A";
//         tf_IIIduanDongzuo.GetChild(0).GetComponent<Text>().text = PinjieDianliu(15) + "A " + "0.006A 0.004A";
        f_IduanTime = float.Parse(PinjieDianliu(12)) + ran.Next(2, 20) / 100.0f;
        f_IIduanTime = float.Parse(PinjieDianliu(14)) + ran.Next(2, 20) / 100.0f;
        f_IIIduanTime = float.Parse(PinjieDianliu(16)) + ran.Next(2, 20) / 100.0f;
        //判断是否>=10，是的话保留2位小数，不是的话保留3位小数
        if (isDayuTen(f_IduanTime)) {
            tf_IduanDongzuo.GetChild(1).GetComponent<Text>().text = (f_IduanTime).ToString("0.00") + "‘’过流I段动作";
        }else
            tf_IduanDongzuo.GetChild(1).GetComponent<Text>().text = (f_IduanTime).ToString("0.000") + "‘’过流I段动作";
        if (isDayuTen(f_IIduanTime)) {
            tf_IIduanDongzuo.GetChild(1).GetComponent<Text>().text = (f_IIduanTime).ToString("0.00") + "‘’过流II段动作";
        }else
            tf_IIduanDongzuo.GetChild(1).GetComponent<Text>().text = (f_IIduanTime).ToString("0.000") + "‘’过流II段动作";
        if (isDayuTen(f_IIIduanTime)) {
            tf_IIIduanDongzuo.GetChild(1).GetComponent<Text>().text = (f_IIIduanTime).ToString("0.00") + "‘’过流III段动作";
        }else
            tf_IIIduanDongzuo.GetChild(1).GetComponent<Text>().text = (f_IIIduanTime).ToString("0.000") + "‘’过流III段动作";
    }

    /// <summary>
    /// 把分开的数字拼接
    /// </summary>
    /// <param name="i">索引</param>
    /// <returns></returns>
    public string PinjieDianliu(int i) {
        string str = "";
        for (int index = 0; index <= 4; index++) {
            str += DingzhiZhengdingQuUIController._instance.tf_ContentRight.GetChild(i).GetChild(index).GetChild(1).GetComponent<Text>().text;
        }
        return str;
    }

     /// <summary>
     /// 判断得到的数字是否>=10
     /// </summary>
     /// <param name="num"></param>
     /// <returns></returns>
     private bool isDayuTen(float num) {
        if (num >= 10.0f) {
            return true;
        }
        return false;
    }

    private void OnQuitButtonClick(bool obj)
    {
        if (tf_AllUI.GetChild(i_AllUIChildCount - 1).name == "shijianjiluGO") {
            for (int i = 0; i < i_AllUIChildCount; i++)
            {
                if (tf_AllUI.GetChild(i).name == "mainUI")
                {
                    TimerMngr.Instance.Create(() =>
                    {
                        EventManager<bool>.TriggerEvent(MyEventType.ChongzhiShujuEvent, true);
                        tf_AllUI.GetChild(i).SetAsLastSibling();
                    }).Start(0.1f);
                    break;
                }
            }
        }
    }

    private void OnSetButtonClick(bool obj)
    {
        if (tf_AllUI.GetChild(i_AllUIChildCount - 1).name == "shijianjiluGO")
        {
            for (int i = 0; i < i_AllUIChildCount; i++)
            {
                if (tf_AllUI.GetChild(i).name == "mainUI")
                {
                    TimerMngr.Instance.Create(() =>
                    {
                        EventManager<bool>.TriggerEvent(MyEventType.ChongzhiShujuEvent,true);
                        tf_AllUI.GetChild(i).SetAsLastSibling();
                    }).Start(0.1f);
                    break;
                }
            }
        }
    }

    private void OnDownButtonClick(bool obj)
    {
        if (tf_AllUI.GetChild(i_AllUIChildCount - 1).name == "shijianjiluGO")
        {
            if (OnllyPage2UIController._instance.testType == "程控")
            {
                if (sb_Content.value > 6.5f / 7.0f)
                {
                    sb_Content.value -= 1.0f / 7.0f;
                }
                else if (sb_Content.value <= 0.001f)
                {
                    sb_Content.value = 1;
                }
                else
                {
                    sb_Content.value -= 3.0f / 7.0f;
                }
            }else
            {
                if (sb_ContentSK.value <= 0.001f)
                    sb_ContentSK.value = 1;
                else
                    sb_ContentSK.value = 0;
            }
        }
    }

    private void OnUpButtonClick(bool obj)
    {
        if (tf_AllUI.GetChild(i_AllUIChildCount - 1).name == "shijianjiluGO")
        {
            if (OnllyPage2UIController._instance.testType == "程控")
            {
                if (sb_Content.value >= (1 - 1.0f / 7.0f))
                {
                    sb_Content.value += 1.0f / 7.0f;
                }
                else
                {
                    sb_Content.value += 3.0f / 7.0f;
                }
                if (sb_Content.value >= 1)
                {
                    sb_Content.value = 1;
                }
            }
            else {
                sb_ContentSK.value = 1;
            } 
        }
    }
}
